<?php
// +----------------------------------------------------------------------
// | BB [ BB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2024~2034 https://www.bobahappy.com.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: luowei Team <luowei@bobahappy.com>
// +----------------------------------------------------------------------


namespace app\services\platform;


use app\dao\platform\${tableClass}Dao;
use app\services\BaseServices;
use crmeb\services\FormBuilder as Form;
use crmeb\utils\Arr;
use think\facade\Route as Url;

/**
 * ${tableComment}service
 * Class ${tableClass}Services
 * @package app\services\platform
 */
class ${tableClass}Services extends BaseServices
{
    public function __construct(${tableClass}Dao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 获取列表
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList($keywords, int $page = 0, int $limit = 0)
    {
        return $this->dao->getList($keywords,$page,$limit);
    }
	

	/**
	 * 修改
	 * @param array $data
	 * @return mixed
	 */
	public function save(array $data)
	{
		if($data['id']){
			$data['updated_time'] = date('Y-m-d H:i:s');
			$data['updated_uid'] = request()->adminId();
			return $this->dao->update($data['id'], $data);
		}else{
			$data['created_uid'] = request()->adminId();
			return $this->dao->save($data);
		}
	    
	}
	<#if formWay == 0>
		/**
	 * 生成创建修改表单
	 * @param int $id
	 * @return array
	 * @throws \FormBuilder\Exception\FormBuilderException
	 */
	public function add(int $id)
	{
	    $method = 'POST';
	    $url = '/${shortName}/save';
	    $data = $this->dao->get($id);
        $field = array();
        if (!$data) {
            $title = '添加';
            
	        <#list tableProperties as value>
		    <#if value.columnName != 'id'>
		    <#if value.columnName == 'status' && statusType==1>
		    $field[] = Form::radio('status', '状态', 0)->options([['label' => '启用', 'value' => 0], ['label' => '停用', 'value' => 1]]);
		    <#else>
		    $field[] = Form::input('${value.columnName}', '${value.comment}', '')->maxlength(50);
		    </#if>
		    </#if>
		   </#list>
        } else {
            $title = '修改';
            $field[] = Form::hidden('id', $id);
	        <#list tableProperties as value>
		    <#if value.columnName != 'id'>
			<#if value.columnName == 'status' && statusType==1>
		    $field[] = Form::radio('status', '状态', $data['status'])->options([['label' => '启用', 'value' => 0], ['label' => '停用', 'value' => 1]]);
		    <#else>
		    <#if value.columnName == 'name' || value.columnName == 'title'>
		    $field[] = Form::input('${value.columnName}', '${value.comment}', $data['${value.columnName}'] ?? '')->maxlength(50)->required('请填写${value.comment}');
		    <#else>
		    $field[] = Form::input('${value.columnName}', '${value.comment}', $data['${value.columnName}'] ?? '')->maxlength(50);
		    </#if>
		    </#if>
			</#if>
		   </#list>
        }
	    return create_form($title, $field, Url::buildUrl($url), $method);
	}
	</#if>
	
	
	/**
	 * 修改
	 * @param array $data
	 * @return mixed
	 */
	public function del($id)
	{
		return $this->dao->update($id, [
			'is_del' => 1
		]);
	    
	}
	
	<#list methods as value>
	/**
	 * ${value.methodName}
	 * @param array $where
	 * @return array
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function ${value.methodName}()
	{
	    return $this->search([])->field('${value.properties}')->where('is_del',0)->order('id desc')->select()->toArray();
	}	
	</#list>
	
}


